import { message } from "antd"
const baseUrl = "http://jacklv.cn/"
export default function request(url, options = {}) {
    return fetch(baseUrl + url, {
        method: options.method ? options.method : "GET",
        body: JSON.stringify(options.data),
        headers: {
            "Content-Type": "application/json",
            token: localStorage.token
        }
    })
        .then(response => {
            switch(response.status){
                case 200:
                    return response.json()
                case 401:
                    localStorage.clear()
                    message.error("身份认证失败，即将跳转登录页面")
                    setTimeout(function(){
                        window.location.assign("/login")
                    },2000)
                    return Promise.reject(response)
                default:
                    return Promise.reject(response)
            }
        })
        .then(res => {
            if (res.code == 1) return res.data
            else {
                message.error(res.msg || "接口错误")
                return Promise.reject(res)
            }
        })
}